////////////////////////////////////////////////////////////////////////////////
//   Copyright GRGBanking Platform 2011 
//   All rights reserved. 
////////////////////////////////////////////////////////////////////////////////
package utils
{

	/*========================================================*/
	/*   Import                                               */
	/*========================================================*/

	import mx.collections.ArrayCollection;
	import mx.controls.dataGridClasses.DataGridColumn;

	public class DataGridUtil
	{

		/*========================================================*/
		/*   Constructor                                          */
		/*========================================================*/

		public function DataGridUtil()
		{
		}

		/*========================================================*/
		/*   Static Function                                      */
		/*========================================================*/

		/**
		 * 从一个对象列表中,拿出一个属性列表
		 */
		public static function getPropertys(property:String, arr:Array):ArrayCollection
		{
			var length:int = arr.length;
			var propertyArr:ArrayCollection = new ArrayCollection();
			for (var index:int = 0; index < length; index++)
			{
				if (arr[index].hasOwnProperty(property))
					propertyArr.addItem(arr[index][property]);
			}
			return propertyArr;
		}

		/**
		 * 从一个对象列表中,拿出一个属性列表
		 */
		public static function getItemsValue(property:String, arr:Array):ArrayCollection
		{
			var length:int = arr.length;
			var propertyArr:ArrayCollection = new ArrayCollection();
			for (var index:int = 0; index < length; index++)
			{
				var value:* = getItemValue(arr[index], property);
				if (value)
					propertyArr.addItem(value);
			}
			return propertyArr;
		}		
		
		/**
		 * 格式化时间
		 * @param p_item
		 * @param p_column
		 * @return 
		 * 
		 */		
		public static function formatTime(p_item:Object, p_column:DataGridColumn):String
		{
			return DateFormatter.format(convertToDate(p_item, p_column), "YYYY-MM-DD JJ:NN:SS");
		}	
		
		public static function formatShortTime(p_item:Object, p_column:DataGridColumn):String
		{
			return DateFormatter.format(convertToDate(p_item, p_column), "YYYY-MM-DD JJ:NN:SS");
		}
		
		
		/**
		 * 格式化日期
		 * @param p_item
		 * @param p_column
		 * @return 
		 * 
		 */		
		public static function formatDay(p_item:Object, p_column:DataGridColumn):String
		{
			return DateFormatter.format(convertToDate(p_item, p_column), "YYYY-MM-DD");
		}
		
		/**
		 * 
		 * 
		 */		
		private static function convertToDate(p_item:Object, p_column:DataGridColumn):Date
		{
			var p_item_value:Object = getItemValue(p_item, p_column.dataField);
			if(!p_item_value) return null;
			var timeString:String = p_item_value.toString();
			return new Date(timeString);
		}
		
		/**
		 * 
		 * @param p_item
		 * @param p_column
		 * @return 
		 * 
		 */		
		public static function getItemValue(p_item:Object, p_dataField:String):Object
		{
			var fields:Array = p_dataField.split(".");
			var obj:Object = p_item;
			for(var i:int = 0; i < fields.length; i++)
			{
				obj = obj[fields[i]];
				if(obj == null){
					return null;
				}
			}
			return obj;
		}

		/**
		 * 
		 * 
		 */		
		public static function setPropertys():void
		{

		}
	}
}